<?php


use Phinx\Migration\AbstractMigration;

class FillSequenceShotFxClothFAttributeData extends AbstractMigration
{

    protected $attributeTable;

    protected $attributeValueTable;

    protected $stepCategoryAttributeTable;

    /**
     * 获取所有工序分类数据
     * @return array
     */
    public function getAllStepCategoryData()
    {
        $sql = 'select * from step_category';
        $result = $this->query($sql);
        $info = array();
        foreach ($result as $key => $val) {
            $info[$val['code']] = $val;
        }
        return $info;
    }


    /**
     * 递归添加属性和属性值
     * @param $attributeData
     * @param $allStepCategoryData
     * @throws Exception
     */
    public function recursionAttribute($attributeData, $allStepCategoryData)
    {
        foreach ($attributeData as $attributeItem) {
            $attributeAdd = $attributeItem['data'];
            $attributeAdd['uuid'] = Webpatser\Uuid\Uuid::generate()->string;
            $this->attributeTable->insert($attributeAdd)->save();
            $query = $this->fetchRow('SELECT max(`id`) as id FROM attribute');

            // 工序分类关联属性
            foreach ($attributeItem['module_code'] as $moduleCode) {
                $stepCategoryAttributeItem = [
                    'attribute_id' => $query['id'],
                    'step_category_id' => $allStepCategoryData[$moduleCode]['id']
                ];

                $this->stepCategoryAttributeTable->insert($stepCategoryAttributeItem)->save();
            }

            foreach ($attributeItem['values'] as $value => $variable) {
                $attributeValueAdd = [
                    'value' => $value,
                    'attribute_id' => $query['id'],
                    'variable' => $variable,
                    'uuid' => Webpatser\Uuid\Uuid::generate()->string
                ];
                $this->attributeValueTable->insert($attributeValueAdd)->save();
            }
        }
    }


    public function change()
    {
        $attributeData = [
            [
                'module_code' => ['shot'],
                'data' => [
                    'name' => '特效类型',
                    'code' => 'texiaoleixing'
                ],
                'values' => [
                    '引擎特效' => 1,
                    '混合特效' => 1,
                    '其他特效' => 1,
                    '无特效' => 1,
                ]
            ],
            [
                'module_code' => ['sequence'],
                'data' => [
                    'name' => '布料解算',
                    'code' => 'buliaojiesuan'
                ],
                'values' => [
                    '高质量解算' => 1,
                    '引擎解算' => 1,
                ]
            ],
        ];


        $allStepCategoryData = $this->getAllStepCategoryData();
        $this->attributeTable = $this->table('attribute');
        $this->attributeValueTable = $this->table('attribute_value');
        $this->stepCategoryAttributeTable = $this->table('step_category_attribute');

        $this->recursionAttribute($attributeData, $allStepCategoryData);
    }


}
